import 'package:sl_fast_dev/sl_fast_dev.dart';
import 'package:sl_fast_dev/src/dao/sl_node.dart';
import 'package:sl_fast_dev/src/template/sl_templete.dart';
import 'package:sl_fast_dev/src/utils/sl_consts.dart';

class SLObject {
  static String property(SLNode node, SLNode fatherNode) {
    var key = 'strong', type = node.type, name = node.property();

    if (node.nodeFor == SLNodeFor.SLNodeForVM) {
      key = 'copy';
      type = SLNodeType.NSString;
      name = node.nameString();
    }

    return '/// ${fatherNode.cls()}.${node.property()}\n '
        '@property (nonatomic, $key) $type *$name;';
  }

  static String propertyList(String name, String type) =>
      '@property (nonatomic, strong) NSArray<$type *> *$name;';

  static String propertyVM(SLNode node, SLNode fatherNode) =>
      '/// ${fatherNode.cls()}.${node.property()}\n '
      '@property (nonatomic, strong) ${SLNodeType.RACCommand}* ${node.nameCMD()};';

  static List<String> sub_view(SLNode node, SLNode superNode) {
    return [
      '    [${super_view(superNode)} addSubview:self.${node.property()}];'
    ];
  }

  static String layout(SLNode node, SLNode superNode) {
    var mas = sl_mas;

    mas = mas.replaceAll('[MAS_SUPER]', super_view(superNode));
    mas = mas.replaceAll('[MAS_NAME]', node.property());

    return mas;
  }

  static String super_view(SLNode node) {
    var father = 'self';
    if (node.isUITableViewCell()) father = 'self.contentView';

    return father;
  }

  static String table_cell(SLNode node) =>
      '${node.cls()} *cell = [${node.cls()} cellWithTableView:tableView indexPath:indexPath];';

  static String cell_regis(SLNode node, String name) =>
      '        [_$name registerClass:[${node.cls()} class] forCellReuseIdentifier:[${node.cls()} reuseIdentifier]];';

  static String kit(String type) {
    if (type == SLNodeType.RACCommand) return sl_kit_RACCommand;
    if (type == SLNodeType.RACCommand) return sl_kit_RACCommand;
    if (type == SLNodeType.UIButton) return sl_kit_UIButton;
    if (type == SLNodeType.UIImageView) return sl_kit_UIImageView;
    if (type == SLNodeType.UILabel) return sl_kit_UILabel;
    if (type == SLNodeType.UIScrollView) return sl_kit_UIScrollView;
    if (type == SLNodeType.UITableView) return sl_kit_UITableView;
    if (type == SLNodeType.UITextField) return sl_kit_UITextField;
    if (type == SLNodeType.UIView) return sl_kit_UIView;

    return sl_kit_Class;
  }

  static String property_get(SLNode node) {
    var type =
        (node.hasChildren() && !node.isContainer()) ? 'Class' : node.type;

    var file = kit(type);

    file = file.replaceAll('[${type}_NAME]', node.property());
    if (node.text != null) file = file.replaceAll('[${type}_TEXT]', node.text);
    file = file.replaceAll('[${type}_SIZE]', SLFastDevConfig().fontSize);
    file = file.replaceAll('[${type}_TYPE]', node.cls());

    if (node.isUITableView()) {
      var cells = [];
      node.getAllUITableViewCells().forEach((el) {
        cells.add(cell_regis(el, node.property()));
      });
      if (cells.isNotEmpty) {
        file = file.replaceAll('[${type}_CELL_GEGIS]', cells.join('\n'));
      }
    }

    return file;
  }

  static String vm_bind(SLNode node) {
    if (!node.canBindRAC()) return '';

    var file = bind(node.type);

    file = file.replaceAll('[${node.type}_NAME]', node.property());
    file = file.replaceAll('[${node.type}_String]', node.nameString());
    file = file.replaceAll('[${node.type}_CMD]', node.nameCMD());

    return file;
  }

  static String bind(String type) {
    if (type == SLNodeType.UIButton) return sl_vm_bind_UIButton;
    if (type == SLNodeType.UILabel) return sl_vm_bind_UILabel;
    if (type == SLNodeType.UITextField) return sl_vm_bind_UITextField;

    return '';
  }
}
